//
//  YQForumViewController.m
//  iyuanqi
//
//  Created by czl on 2017/7/27.
//  Copyright © 2017年 czl. All rights reserved.
//

#import "YQForumViewController.h"
#import "ZLCollectionViewFlowLayout.h"
#import "YQForumHeaderView.h"
#import "YQBannerObject.h"
#import "YQForumCell.h"
#import "YQArticleListViewController.h"

@interface YQForumViewController ()
<ZLCollectionViewFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
    
@property(nonatomic,strong)UICollectionView* collectionViewForum;
@property(nonatomic,copy)NSArray* arrayForums;
@property(nonatomic,copy)NSArray* arrayBanners;
@property(nonatomic,strong)NSMutableArray* arrayImageUrls;

@end

@implementation YQForumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"厦门缘起公益相亲平台";
    
    [self.view addSubview:self.collectionViewForum];
    [self.collectionViewForum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [YQWebManager getForumList:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            NSArray* array = response[@"data"];
            if ([array isKindOfClass:[NSArray class]]) {
                self.arrayForums = [YQForumObject mj_objectArrayWithKeyValuesArray:array];
                [self.collectionViewForum reloadData];
            }
        }
    } withFailureBlock:^(NSError *error) {
        
    }];
    
    self.arrayImageUrls = [NSMutableArray new];
    [YQWebManager getBanners:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            NSArray* array = response[@"data"];
            if ([array isKindOfClass:[NSArray class]]) {
                self.arrayBanners = [YQBannerObject mj_objectArrayWithKeyValuesArray:array];
                if (self.arrayBanners.count > 0) {
                    for (YQBannerObject *obj in self.arrayBanners) {
                        [self.arrayImageUrls addObject:[Image_url stringByAppendingString:obj.attachment]];
                    }
                    [self.collectionViewForum reloadData];
                }
            }
        }
    } withFailureBlock:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrayForums.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YQForumCell* cell = [YQForumCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    YQForumObject* object = _arrayForums[indexPath.row];
    [cell setForumObject:object];
    return cell;
}

- (ZLLayoutType)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout *)collectionViewLayout typeOfLayout:(NSInteger)section {
    return ClosedLayout;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout*)collectionViewLayout columnCountOfSection:(NSInteger)section {
    return 2;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(collectionView.width, 250);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString : UICollectionElementKindSectionHeader]){
        YQForumHeaderView* headerView = [YQForumHeaderView headerViewWithCollectionView:collectionView forIndexPath:indexPath];
        [headerView setImageUrls:_arrayImageUrls];
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YQForumObject* object = _arrayForums[indexPath.row];
    YQArticleListViewController* vc = [[YQArticleListViewController alloc]initWithFid:object];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UICollectionView*)collectionViewForum {
    if (!_collectionViewForum) {
        ZLCollectionViewFlowLayout *layout = [[ZLCollectionViewFlowLayout alloc]init];
        layout.delegate = self;
        layout.sectionInset = UIEdgeInsetsZero;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(200, 100);
        
        _collectionViewForum = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionViewForum.delegate = self;
        _collectionViewForum.dataSource = self;
        _collectionViewForum.backgroundColor = [UIColor whiteColor];
        [_collectionViewForum registerClass:[YQForumCell class] forCellWithReuseIdentifier:[YQForumCell cellIdentifier]];
        [_collectionViewForum registerClass:[YQForumHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[YQForumHeaderView headerViewIdentifier]];
    }
    return _collectionViewForum;
}

@end
